<?php

namespace Portal\SearchBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use Portal\MainBundle\Utils\customFunctions;

class SearchController extends Controller
{
    public function indexAction()
    {
      
      $pageName = 'page';
      if (!isset($_GET['page'])) {
        $page = 1;
      } else {
        $page = (int) $_GET['page'];
      }
      $orderBy = (int) $this->getRequest()->request->get('orderBy');
      if('POST' == $this->getRequest()->getMethod()){
        
        if($orderBy > 0)
        {
          $tutorList = $this->getDoctrine()->getManager()->getRepository('PortalSearchBundle:Tutor')->getAllTutorByStatus($this->container->getParameter('statusActive'), $this->container->getParameter('perPageOnSearch'), $page);
          $sortBy = $orderBy;
          $countTutor = $this->getDoctrine()->getManager()->getRepository('PortalSearchBundle:Tutor')->countTutor($this->container->getParameter('statusActive'));
        }else{
          $tutorList = $this->getDoctrine()->getManager()->getRepository('PortalSearchBundle:Tutor')->getAllTutorByStatus($this->container->getParameter('statusActive'), $this->container->getParameter('perPageOnSearch'), $page);
          $sortBy = $this->container->getParameter('tutorSearchOrderBy1');
          $countTutor = $this->getDoctrine()->getManager()->getRepository('PortalSearchBundle:Tutor')->countTutor($this->container->getParameter('statusActive'));
        }
        
      }else{      
        $tutorList = $this->getDoctrine()->getManager()->getRepository('PortalSearchBundle:Tutor')->getAllTutorByStatus($this->container->getParameter('statusActive'), $this->container->getParameter('perPageOnSearch'), $page);        
        $sortBy = $orderBy;
        $countTutor = $this->getDoctrine()->getManager()->getRepository('PortalSearchBundle:Tutor')->countTutor($this->container->getParameter('statusActive'));
      }
      
//      var_dump($tutorList);die;
      
      $lessonCount = $this->getDoctrine()->getManager()->getRepository('PortalSearchBundle:TutorSubject')->countLesson();
      
      return $this->render('PortalSearchBundle:Search:index.html.twig', array(
                'tutorList' => $tutorList,
                'sortBy' => $sortBy,
                'countTutor'  => $countTutor,
                'page' => $page,
                'per_page' => $this->container->getParameter('perPageOnSearch'),
                'tutorCount' => $countTutor,
                'tutorLenght' => $countTutor,
                'lessonCount' => $lessonCount,
                'lessonLenght' => $lessonCount,
                
            ));
    }
    
    public function searchPagerAction($page, $per_page, $total, $pageName, $delta = 0)
    {
      echo customFunctions::pagination($page, $per_page, $total, $pageName);

      return $this->render('PortalSearchBundle:Search:pager.html.twig', array(
                  'pager' => ''
              ));
    }
}
